package org.lg.controller;

import org.lg.entity.User;
import org.lg.service.impl.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    // 注册
    @PostMapping(value = "/register", consumes = "application/json", produces = "application/json")
    public Map<String, Object> registerUser(@RequestBody User user) {
        String msg = userService.registerUser(user);
        int code = "用户注册成功!".equals(msg) ? 200 : 409;
        return Map.of("code", code, "msg", msg);
    }

    // 登录
    @PostMapping(value = "/login", produces = "application/json")
    public Map<String, Object> loginUser(@RequestParam String username,
                                         @RequestParam String password) {
        String msg = userService.loginUser(username, password);
        int code = "用户登录成功!".equals(msg) ? 200 : 401;
        return Map.of("code", code, "msg", msg);
    }
    //留白逻辑
}

